// import { getToken } from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import { toast, showConfirm, tansParams } from '@/utils/common'
import Encrypt from "@/utils/encrypt";
const aes = new Encrypt();
const timeout = 10000
// const baseUrl = import.meta.env.VITE_PROJ_BASE_URL
const apiServer = 'http://59.110.53.153:7001/'
const request = config => {
    if (config.loading !== false) {
        uni.showLoading({
            title: '请稍后..',
            mask: true,
        })
    }
    // 是否需要设置 token
    const noToken = (config.headers || {}).isToken === false
    const timeStamp = new Date().getTime();
    /* if (getToken() && !isToken) {
        config.header['Authorization'] = 'Bearer ' + getToken()
    } */
    // get请求映射params参数
    if (config.params) {
        let url = `${config.url}?${tansParams(config.params)}`
        url = url.slice(0, -1)
        config.url = url
    }
    const params = {
        uri: config.url,
        param: { ...config.data }
    }
    // const headers = {
    //     "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
    //     "time-stamp": timeStamp,
    //     "auth": JSON.stringify({
    //         "userToken": noToken ? '' : uni.getStorageSync('token'),
    //         "deviceId": "",
    //         "deviceType": "WX",
    //         "deviceVersion": uni.getStorageSync('system'),
    //         "appVersion": "1.0"
    //     })
    // }
    // headers.auth = aes.encode(headers.auth, timeStamp);
    console.log('params', params)
    // if (import.meta.env.MODE == 'prodution') {
    // const data = { data: aes.encode(JSON.stringify(params), timeStamp) }
    // }
    return new Promise((resolve, reject) => {
        uni.request({
            method: config.method || 'get',
            timeout: config.timeout || timeout,
            // url: config.baseUrl || baseUrl + config.url,
            url: apiServer + config.url,
            data: { ...config.data },
            // header: headers,
            // dataType: 'json'
        }).then(response => {
            resolve(response.data)
        })
        .catch(error => {
            reject(error)
        }).finally(_ => {
            if (config.loading !== false) {
                uni.hideLoading()
            }
        })
    })
}
export default request
